package models

import "gorm.io/gorm"

type CasBinRule struct {
	PType string `json:"p_type" db:"p_type"`
	V0    string `json:"v0" db:"v0"`
	V1    string `json:"v1" db:"v1"`
	V2    string `json:"v2" db:"v2"`
	V3    string `json:"v3" db:"v3"`
	V4    string `json:"v4" db:"v4"`
	V5    string `json:"v5" db:"v5"`
}

func (CasBinRule) TableName() string {
	return "casbin_rules"
}

func DeletePermission(tx *gorm.DB, roleKey string) (err error) {
	return tx.Table("casbin_rules").Where("v0 = ?", roleKey).Delete(&CasBinRule{}).Error
}

func DeletePermissionByPaths(tx *gorm.DB, paths []string) (err error) {
	return tx.Table("casbin_rules").Where("v1 in (?)", paths).Delete(&CasBinRule{}).Error
}

func UpdatePermissionByPath(tx *gorm.DB, existPath, newPath, action string) error {
	return tx.Table("casbin_rules").
		Where("v1 = ? and v2 = ?", existPath, action).
		Update("v1", newPath).Error
}
